package com.lams.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateHelper {

	public static String formatDateTime(Date date, String format) {
		if (date == null)
			return null;
		if (format == null)
			return date.toString();
		DateFormat df = new SimpleDateFormat(format);
		return df.format(date);
	}

	public static String formatY(Date date) {
		return date == null ? "" : formatDateTime(date, "yyyy");
	}

	public static String formatYM(Date date) {
		return date == null ? "" : formatDateTime(date, "yyyy-MM");
	}

	public static String formatYMD(Date date) {
		return date == null ? "" : formatDateTime(date, "yyyy-MM-dd");
	}

	public static String formatYMDHM(Date date) {
		return date == null ? "" : formatDateTime(date, "yyyy-MM-dd HH:mm");
	}

	public static String formatYMDHMS(Date date) {
		return date == null ? "" : formatDateTime(date, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 将日期字符串转换为 Date类型
	 * 
	 * @param dateStr
	 * @param pattern
	 * @return
	 */
	public static Date parseDate(String dateStr, String pattern) {
		DateFormat df = new SimpleDateFormat(pattern);

		Date result = null;
		try {
			result = df.parse(dateStr);
		} catch (ParseException e) {
		}

		return result;
	}
}
